package com.lazyboyl.sharding.proxy.net.frontend;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author linzf
 * @since 2021-06-30
 * 类描述： navicat连接收集器，意味着有几个navicat连接了我们的服务器
 */
public class FrontendGroupHandler extends ChannelHandlerAdapter {

    private static final Logger logger = LoggerFactory.getLogger(FrontendGroupHandler.class);

    protected FrontendConnection connection;

    public FrontendGroupHandler(FrontendConnection connection) {
        this.connection = connection;
    }

    /**
     * 用于存储所有的与服务器产生连接的登录对象
     */
    public static ConcurrentHashMap<Long, FrontendConnection> frontendGroup = new ConcurrentHashMap();

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        logger.info("编号为{}的连接连接上", connection.getId());
        frontendGroup.put(connection.getId(), connection);
        ctx.fireChannelActive();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        logger.info("编号为{}的连接断开", connection.getId());
        frontendGroup.remove(connection.getId());
        connection.close();
        ctx.fireChannelActive();
    }

}
